1 <Window x:Class="CSWPFDataBinding.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:local ="clr-namespace:CSWPFDataBinding"
5 Title="MainWindow" Height="600" Width="600">
9 <ObjectDataProvider x:Key="defaultPerson" ObjectType="{x:Type local:Person}"></ObjectDataProvider>
10 <ObjectDataProvider x:Key="persons" ObjectType="{x:Type local:Persons}"></ObjectDataProvider>
11 <CollectionViewSource x:Key="personSource" Source="{StaticResource persons}"></CollectionViewSource>
13 <local:SalaryFormmatingConverter x:Key="con"></local:SalaryFormmatingConverter>
16 <Style TargetType="{x:Type Border}" >
18 <Setter Property="BorderBrush" Value="Blue"></Setter>
19 <Setter Property="BorderThickness" Value="1.0"></Setter>
20 <Setter Property="Background" Value="White"></Setter>
21 <Setter Property="CornerRadius" Value="10"></Setter>
24 <Trigger Property="IsMouseOver" Value="True">
25 <Setter Property="BorderThickness" Value="2.0"></Setter>
26 <Setter Property="BorderBrush" Value="Yellow"></Setter>
30 <!-- Error Template for TextBox-->
31 <ControlTemplate x:Key="errorTemplate">
33 <AdornedElementPlaceholder></AdornedElementPlaceholder>
34 <TextBlock Foreground="Red" FontSize="20" Text="!"></TextBlock>
38 <Grid Background="White">
39 <Grid.ColumnDefinitions>
40 <ColumnDefinition></ColumnDefinition>
41 <ColumnDefinition></ColumnDefinition>
42 </Grid.ColumnDefinitions>
44 <RowDefinition Height="Auto">
46 <RowDefinition Height="Auto">
48 <RowDefinition Height="Auto">
50 <RowDefinition Height="Auto">
52 <RowDefinition Height="Auto">
54 <RowDefinition Height="Auto">
56 <RowDefinition Height="Auto">
58 <RowDefinition Height="Auto">
60 </Grid.RowDefinitions>
62 <TextBlock Text="Binding using XAML code:" Grid.Row="0" Grid.ColumnSpan="2"></TextBlock>
63 <TextBlock Text="Binding using Procedure code:" Grid.Row="2" Grid.ColumnSpan="2"></TextBlock>
64 <TextBlock Text="CollectionBinding using Xaml" Grid.Row="4" Grid.ColumnSpan="2"></TextBlock>
65 <TextBlock Text="CollectionBinding using Procedure Code" Grid.Row="6" Grid.ColumnSpan="2"></TextBlock>
67 <Border Grid.Column="0" Grid.Row="1">
68 <StackPanel Margin="10" >
69 <!--Twoway binding, and the change will be propagated to the source to target once propery is changed.-->
70 <StackPanel Orientation="Horizontal" >
72 <TextBox Text="{Binding Name, Source ={StaticResource defaultPerson}, Mode=Twoway,
73 UpdateSourceTrigger=PropertyChanged }" Height="25" Width="222"></TextBox>
75 <!--The default UpdateSourceTrigger property for Text is LostFocus, you will find source will be updated when the Textbox losts focus.
76 You will see Textbox will not load the defalut value when it is loaded due to it uses OneWayToSouce binding mode.
78 <StackPanel Orientation="Horizontal" >
79 <Label Width="44" Height="25">Job:</Label>
80 <TextBox Text="{Binding Job, Source={StaticResource defaultPerson}, Mode = OneWayToSource}" Height="26" Width="222"></TextBox>
82 <!--If age is less than 0, or much bigger than 300. There will be "!" appearing next to TextBox indicating the input error-->
83 <StackPanel Orientation="Horizontal" >
84 <Label Height="26" Width="44">Age:</Label>
85 <TextBox Height="26" Width="222" Validation.ErrorTemplate="{StaticResource errorTemplate}">
87 <Binding Path="Age" Source="{StaticResource defaultPerson}">
88 <Binding.ValidationRules>
89 <local:AgeValidationRule></local:AgeValidationRule>
90 </Binding.ValidationRules>
97 <StackPanel Orientation="Horizontal" >
98 <Label Height="26" Width="44">Salary:</Label>
99 <TextBox Text="{Binding Salary, Source={StaticResource defaultPerson}, Mode=Twoway, UpdateSourceTrigger =PropertyChanged}"
100 Height="26" Width="222"></TextBox>
103 <!--The target will be updated only if we change the source if the binding mode is Oneway-->
104 <StackPanel Orientation="Horizontal" >
105 <Label Height="23" Width="53">Interest:</Label>
106 <TextBox Text="{Binding Interest, Source={StaticResource defaultPerson}, Mode =Oneway}" Height="26" Width="222"></TextBox>
110 <Border Grid.Column="1" Grid.Row="1">
111 <StackPanel Margin="10" >
112 <StackPanel Orientation="Horizontal">
114 <Label Content="{Binding Name, Source ={StaticResource defaultPerson}}" Height="25" Width="222"></Label>
116 <StackPanel Orientation="Horizontal">
117 <Label Width="44" Height="25">Job:</Label>
118 <Label Content="{Binding Job, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
120 <StackPanel Orientation="Horizontal">
121 <Label Height="26" Width="44">Age:</Label>
122 <Label Content="{Binding Age, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
124 <StackPanel Orientation="Horizontal" >
125 <Label Height="26" Width="44">Salary:</Label>
126 <!--Here We use the converter to format the salar so that user can see a more readable text-->
127 <Label Content="{Binding Salary, Source={StaticResource defaultPerson}, Converter={StaticResource con}}" Height="26" Width="222"></Label>
129 <StackPanel Orientation="Horizontal">
130 <Label Height="25" Width="56">Interest:</Label>
131 <Label Content="{Binding Interest, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
137 <!--Binding using Procedure code-->
138 <Border Grid.Column="0" Grid.Row="3">
139 <StackPanel Margin="10" >
140 <!--Twoway binding, and the change will be propagated to the source to target once propery is changed.-->
141 <StackPanel Orientation="Horizontal" >
143 <TextBox Name="tbPersonName" Height="26" Width="222"></TextBox>
145 <!--The default UpdateSourceTrigger property for Text is LostFocus, you will find source will be updated when the Textbox losts focus.
146 You will see Textbox will not load the defalut value when it is loaded due to it uses OneWayToSouce binding mode.
148 <StackPanel Orientation="Horizontal" >
149 <Label Width="44" Height="25">Job:</Label>
150 <TextBox Name="tbPersonJob" Text="{Binding Job, Source={StaticResource defaultPerson}, Mode = OneWayToSource}" Height="26" Width="222"></TextBox>
153 <StackPanel Orientation="Horizontal" >
154 <Label Height="26" Width="44">Age:</Label>
155 <TextBox Name="tbPersonAge" Height="26" Width="222" Validation.ErrorTemplate="{StaticResource errorTemplate}">
157 <Binding Path="Age" Source="{StaticResource defaultPerson}">
158 <Binding.ValidationRules>
159 <local:AgeValidationRule></local:AgeValidationRule>
160 </Binding.ValidationRules>
166 <StackPanel Orientation="Horizontal" >
167 <Label Height="26" Width="44">Salary:</Label>
168 <TextBox Name="tbPersonSalary" Text="{Binding Salary, Source={StaticResource defaultPerson}, Mode=Twoway, UpdateSourceTrigger =PropertyChanged}"
169 Height="26" Width="222"></TextBox>
172 <!--The target will be updated only if we change the source if the binding mode is Oneway-->
173 <StackPanel Orientation="Horizontal" >
174 <Label Height="23" Width="53">Interest:</Label>
175 <TextBox Name="tbPersonInterest" Text="{Binding Interest, Source={StaticResource defaultPerson}, Mode =Oneway}" Height="26" Width="222"></TextBox>
179 <Border Grid.Column="1" Grid.Row="3">
180 <StackPanel Margin="10" >
181 <StackPanel Orientation="Horizontal">
183 <Label Name="lblPersonName" Content="{Binding Name, Source ={StaticResource defaultPerson}}" Height="25" Width="222"></Label>
185 <StackPanel Orientation="Horizontal">
186 <Label Width="44" Height="25">Job:</Label>
187 <Label Name="lblPersonJob" Content="{Binding Job, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
189 <StackPanel Orientation="Horizontal">
190 <Label Height="26" Width="44">Age:</Label>
191 <Label Name="lblPersonAge" Content="{Binding Age, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
193 <StackPanel Orientation="Horizontal" >
194 <Label Height="26" Width="44">Salary:</Label>
195 <Label Name="lblPersonSalary" Content="{Binding Salary, Source={StaticResource defaultPerson}, Converter={StaticResource con}}" Height="26" Width="222"></Label>
197 <StackPanel Orientation="Horizontal">
198 <Label Height="25" Width="56">Interest:</Label>
199 <Label Name="lblPersonInterest" Content="{Binding Interest, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
203 <!--Collection Binding Using Xaml-->
204 <Border Grid.Row="5" Grid.ColumnSpan="2">
205 <ListBox DataContext="{StaticResource personSource}" ItemsSource="{Binding .}">
206 <ListBox.ItemTemplate>
208 <StackPanel Orientation="Horizontal">
210 <Label Content="{Binding Name}"></Label>
212 <Label Content="{Binding Job}"></Label>
213 <Label>Interest:</Label>
214 <Label Content="{Binding Interest}"></Label>
217 </ListBox.ItemTemplate>
221 <!--Collection Binding Using Procedure code-->
222 <Border Grid.Row="7" Grid.ColumnSpan="2">
224 <ListBox.ItemTemplate>
226 <StackPanel Orientation="Horizontal">
228 <Label Content="{Binding Name}"></Label>
230 <Label Content="{Binding Job}"></Label>
231 <Label>Interest:</Label>
232 <Label Content="{Binding Interest}"></Label>
235 </ListBox.ItemTemplate>